package cn.edu.neu.springbootdemo.service.impl;

import cn.edu.neu.springbootdemo.mapper.UserMapper;
import cn.edu.neu.springbootdemo.model.User;
import cn.edu.neu.springbootdemo.service.UserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;

    @Override
    public int addUser(User user) {
        try {
            if (mapper.existUser(user)==0){ //如果你数据库没有这个用户，返回0，否则的话返回1
                mapper.addUser(user);
                return 0;//用户不存在，可以注册
            }else {
                return 1;  //用户存在
            }
        }catch (Exception e){
            e.printStackTrace();
            return 2;
        }
    }

    @Override
    public boolean checkUsername(User user) {
        // TODO Auto-generated method stub
        if(mapper.existUser(user)==0){ //如果你数据库没有这个用户，返回0，否则的话返回1
            return true;
        }else {
            return false;
        }
    }

    //使用用户名密码查用户，返回用户id和name
    @Override
    public User existsUser(User user) {
        return mapper.existsUser(user);
    }

    @Override
    public List<User> getUserList(User user) {
        return mapper.getUserList(user);
    }

    @Override
    public User getUser(String userid) {
        return mapper.getUser(userid);
    }
}
